package com.yujistu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TextInputLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AlphaAnimation;
import android.widget.EditText;
import android.widget.ImageView;

import com.andexert.library.RippleView;
import com.dd.CircularProgressButton;
import com.yujistu.R;
import com.yujistu.framework.http.ResultListener;
import com.yujistu.integration.request.LoginRequest;
import com.yujistu.integration.result.LoginResult;
import com.yujistu.utils.Logger;
import com.yujistu.widget.InputMethodRelativeLayout;

/**
 * Created by S3YOGA on 2015/11/3.
 */
public class LoginActivity extends BaseActivity {

    EditText   loginET;
    EditText   mPwdView;

    CircularProgressButton loginCPB;
    InputMethodRelativeLayout  imRL;
    RippleView  registerRV;

    ImageView  image1;
    ImageView  image2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        TextInputLayout  userNameInput = (TextInputLayout) findViewById(R.id.accountTIL);
        TextInputLayout  pwdInput = (TextInputLayout) findViewById(R.id.passwordTIL);

        loginET = userNameInput.getEditText();
        mPwdView = pwdInput.getEditText();

        loginCPB = (CircularProgressButton)findViewById(R.id.loginCPB);
        loginCPB.setIndeterminateProgressMode(true);
        loginCPB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (loginCPB.getProgress() == 0) {
                    loginCPB.setProgress(50);
                    checkInfoAndLogin();
                } else if (loginCPB.getProgress() == 100) {
                    loginCPB.setProgress(0);
                } else {
                    loginCPB.setProgress(100);
                }
            }
        });

        registerRV = (RippleView)findViewById(R.id.registerRV);
        registerRV.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });

       findViewById(R.id.find_pwd).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
               intent.putExtra(RegisterActivity.INTENT_FLAG_IS_FORGET_PASSWORD, true);
               startActivity(intent);
           }
       });

        image1 = (ImageView)findViewById(R.id.picIV1);
        image2 = (ImageView)findViewById(R.id.picIV2);
        mHandler.sendEmptyMessage(0);

        imRL = (InputMethodRelativeLayout)findViewById(R.id.login);
        imRL.setOnSizeChangeListener(mOnSizeChangeListener);
        imRL.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = imRL.getRootView().getHeight() - imRL.getHeight();
            }
        });
    }

    boolean isImage1Showing = false;
    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(isImage1Showing){
                setHideAnimation(image1, 2000);
                setShowAnimation(image2, 2000);
            }else{
                setHideAnimation(image2, 2000);
                setShowAnimation(image1, 2000);
            }
            isImage1Showing = !isImage1Showing;
            mHandler.sendEmptyMessageDelayed(0, 4000);
        }
    };

    private AlphaAnimation  mHideAnimation = null;
    private AlphaAnimation  mShowAnimation = null;

    /**
     * View渐隐动画效果
     */
    private void setHideAnimation(View view, int duration) {
        if(null == view || duration < 0){
            return;
        }
        if(null != mHideAnimation){
            mHideAnimation.cancel();
        }
        mHideAnimation = new AlphaAnimation(1.0f, 0.0f);
        mHideAnimation.setDuration(duration);
        mHideAnimation.setFillAfter(true);
        view.startAnimation(mHideAnimation);
    }

    /**
     * View渐现动画效果
     */
    private void setShowAnimation(View view, int duration) {
       if(null == view || duration < 0){
           return;
       }
       if(null != mShowAnimation){
           mShowAnimation.cancel();
       }
        mShowAnimation = new AlphaAnimation(0.0f, 1.0f);
        mShowAnimation.setDuration(duration);
        mShowAnimation.setFillAfter(true);
        view.startAnimation(mShowAnimation);
    }

   InputMethodRelativeLayout.OnSizeChangeListener mOnSizeChangeListener = new InputMethodRelativeLayout.OnSizeChangeListener() {
       @Override
       public void onSizeChange(boolean paramBoolean, int w, int h) {
           if(paramBoolean){ // 键盘弹出时
               imRL.setPadding(0, -100, 0, 0);

           }else{  // 键盘隐藏时
               imRL.setPadding(0, 0, 0, 0);

           }
       }
   };

    private void checkInfoAndLogin() {
        if(isUserNameValid(loginET.getText().toString()) && isPasswordVaild(mPwdView.getText().toString())){
            loginCPB.setProgress(50);
            doLogin();
        }

        doLogin();
    }

    private boolean isUserNameValid(String s) {
        return false;
    }

    private boolean isPasswordVaild(String s) {
        return false;
    }

    private void doLogin() {
         LoginRequest request = new LoginRequest();
         Logger.i("request:" + request);
         request.cellphone = loginET.getText().toString();
         request.password = mPwdView.getText().toString();
         request.devices = "2";

         getHttpCenter().startHttpRequest(this, request, LoginResult.class, new ResultListener<LoginResult>() {

             @Override
             public void onStart() {
                Logger.i("onStart()");
             }

             @Override
             public void onFinish() {
                 Logger.i("onFinish()");
             }

             @Override
             public void onSuccess(LoginResult mResult) {
                  String error = mResult.getError();
                  if(null == error || TextUtils.isEmpty(error)){


                      loginCPB.setProgress(100);
                      startActivity(new Intent(LoginActivity.this, MainActivity.class));
                      finish();
                  }else{
                      loginCPB.setProgress(0);
                      toast(error, 1000);
                  }
             }

             @Override
             public void onFailed(int returnCode, String returnMsg) {
                 Logger.i("onFailed()");
             }
         });
    }
}
